package com.example.sms_emoticon_library;

import java.io.ByteArrayOutputStream;

import com.example.model.ParcelableSmsPicture;
import com.example.model.SmsPicture;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;

public class FragmentFuntions extends Fragment implements OnClickListener {

	private FuntionsOnClick funtions;
	ImageButton myIBGallerry, myIBCamera, myIBVideo, myIBLocation;
	public static final int REQUEST_LOAD_IMAGE=1; 
	public static final int RESULT_LOAD_IMAGE=2;
	public static final int REQUEST_SENT_PIC=3;
	public static final int RESULT_SENT_PIC=4;
	public static final int REQUEST_SEND_MAP = 5;
	public static final int RESULT_SEND_MAP=6;
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		if(activity instanceof FuntionsOnClick){
			funtions = (FuntionsOnClick) activity;
		}else{
			throw new ClassCastException(activity.toString()+ " must implemenet FragmentFuntions.FuntionsOnCLick");
		}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(container==null){
			return null;
		}		
		return (LinearLayout) inflater.inflate(R.layout.fragment_funtions_layout, container, false);
	}
	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		myIBGallerry= (ImageButton) getActivity().findViewById(R.id.ibGallery);
		myIBCamera = (ImageButton) getActivity().findViewById(R.id.ibCamera);
		myIBVideo= (ImageButton) getActivity().findViewById(R.id.ibVideo);
		myIBLocation= (ImageButton) getActivity().findViewById(R.id.ibLocation);
		myIBGallerry.setOnClickListener(this);
		myIBCamera.setOnClickListener(this);
		myIBVideo.setOnClickListener(this);
		myIBLocation.setOnClickListener(this);
		
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==REQUEST_LOAD_IMAGE && resultCode==Activity.RESULT_OK && data!=null){
			Uri selectedImage = data.getData();
	         String[] filePathColumn = { MediaStore.Images.Media.DATA };
	 
	         Cursor cursor =  getActivity().getContentResolver().query(selectedImage,
	                 filePathColumn, null, null, null);
	         cursor.moveToFirst();
	         
	         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	         String picturePath = cursor.getString(columnIndex);
	         cursor.close();
	         
	         Bitmap myPicture = BitmapFactory.decodeFile(picturePath);
	       
	        // ByteArrayOutputStream bos=  new  ByteArrayOutputStream();
	         //myPicture.compress(Bitmap.CompressFormat.JPEG, 100, bos);
	         //  myPicture.compress(Bitmap.CompressFormat.PNG, 100, bos);
	         //byte[] imageBytes= bos.toByteArray();
	         //String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
	         
	         Intent intentPicture = new Intent(getActivity(), com.example.sms_emoticon_library.PictureSms.class);
	         intentPicture.putExtra("picturePath",  picturePath);
	         //intentPicture.putExtra("encodedImage", encodedImage);
	         //intentPicture.putExtra("abc", myPicture);
	         //intentPicture.putExtra("abc", imageBytes);
	         startActivityForResult(intentPicture, REQUEST_SENT_PIC);
	        
	         //funtions.ibGalarryOnClick(myPicture);  
		}else if (requestCode==REQUEST_SENT_PIC &&resultCode==RESULT_SENT_PIC &&data!=null){	
			 Bundle myData= data.getBundleExtra("DATA");
			 //SmsPicture smsPic= (SmsPicture) myData.getSerializable("SmsPic");
			 //ParcelableSmsPicture parSmsPic= (ParcelableSmsPicture) myData.getParcelable("ParSmsPic");
			 //SmsPicture  getSmsPic= parSmsPic.getSmsPicture();
			 String text= myData.getString("text");
			 String path= myData.getString("path");
			 
			 funtions.ibGalarryOnClick(path,text);  
			 //Toast.makeText(getActivity(), getSmsPic.get_text(), Toast.LENGTH_SHORT).show();
		}else if(requestCode==REQUEST_SEND_MAP && resultCode==RESULT_SEND_MAP&&data!=null){
			Bundle mDataMap= data.getBundleExtra("DataMap");
			Double longitude = mDataMap.getDouble("longitude");
			Double latitude = mDataMap.getDouble("latitude");
			
			funtions.ibCurrentLocation(longitude, latitude);
			
		}
	}

	public interface FuntionsOnClick{
		public void ibGalarryOnClick(String path,String text);
		public void ibCurrentLocation(Double longitude, Double latitude);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v!=null){
			if(v.getId()==R.id.ibGallery){
				getPicture();
			}else if(v.getId()==R.id.ibCamera){
				
			}else if(v.getId()==R.id.ibVideo){
				
			}else if (v.getId()==R.id.ibLocation){
				getMap();
			}
		}
	}
	
	public void  getPicture(){
        Intent intentLoadImage =  new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intentLoadImage, REQUEST_LOAD_IMAGE);	
	}
	
	public void getMap(){
		Intent intentMap = new Intent(getActivity(), com.example.sms_emoticon_library.SendMapActivity.class);
        //intentPicture.putExtra("picturePath",  picturePath);
		startActivityForResult(intentMap, REQUEST_SEND_MAP);
        
	}
	

	
}
